package com.example.zhangwei.ui.refreshlist;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;

import com.example.zhangwei.R;
import com.example.zhangwei.adapter.RefreshAdapter;
import com.example.zhangwei.adapter.SimpleExpandListAdapter;
import com.example.zhangwei.model.ExpandListData;
import com.example.zhangwei.util.BaseActivity;
import com.example.zhangwei.widgts.MyCustomViewGroup;
import com.example.zhangwei.widgts.refreshlist.FreshLayout;
import com.example.zhangwei.widgts.refreshlist.PullToRefreshExpandableListView;
import com.example.zhangwei.widgts.refreshlist.PullToRefreshListView;

public class RefreshListActivity extends BaseActivity implements FreshLayout.PullToRfreshListener,
PullToRefreshListView.PullAndRefreshListViewListener
,PullToRefreshExpandableListView.PullAndRefreshListViewListener
{
	private MyCustomViewGroup mCustomViewGroup;
	
	private FreshLayout mFreshLayout;
	
	private PullToRefreshListView mRefreshList;
	
	private RefreshAdapter mRefreshAdapter;
	
	private PullToRefreshExpandableListView mExpandableListView;
	
	private SimpleExpandListAdapter mExpandListAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.refresh_list_group_view_layout);
		mCustomViewGroup = (MyCustomViewGroup)findViewById(R.id.custom_view_root);	
		
		initFreshlayout();
		
		initRefreshList();
		
		initExpandableList();
	}
	
	private void initFreshlayout()
	{
		mFreshLayout = (FreshLayout)findViewById(R.id.fresh_layout_body);
		mFreshLayout.setPullToRfreshListener(this);
	}
	
	private void initRefreshList()
	{
		mRefreshList = (PullToRefreshListView)findViewById(R.id.fresh_list_view);
		mRefreshList.setPullAndRefreshListViewListener(this);
		
		ImageView image = new ImageView(this);
		image.setBackgroundResource(R.drawable.home4);
		mRefreshList.addHeaderView(image);
		
		mRefreshAdapter = new RefreshAdapter(this,getRefreshData());
		mRefreshList.setAdapter(mRefreshAdapter);
	}
	
	private ArrayList<String> getRefreshData()
	{
		ArrayList<String> list = new ArrayList<String>();
		for(int i = 0;i< 10;i++ )
		{
			String str = "数据" + i;
			list.add(str);
		}
		return list;
	}

	private void initExpandableList()
	{
		mExpandableListView = (PullToRefreshExpandableListView)findViewById(R.id.fresh_expand_list_view);
		mExpandableListView.setPullAndRefreshListViewListener(this);
		
		ImageView image = new ImageView(this);
		image.setBackgroundResource(R.drawable.home4);
		mExpandableListView.addHeaderView(image);
		
		mExpandListAdapter = new SimpleExpandListAdapter(this, 
				(ArrayList<ExpandListData>) getExpandListData());
		mExpandableListView.setAdapter(mExpandListAdapter);
		
	}
	
	private ArrayList<ExpandListData> getExpandListData()
	{
		ArrayList<ExpandListData> list = new ArrayList<ExpandListData>();
		for(int i = 0;i< 10;i++ )
		{
			ExpandListData data = new ExpandListData(); 
			data.name = "标题组" + i;
			data.listString = new String[]{"子集1","子集2","子集3","子集4","子集5","子集6"};
			list.add(data);
		}
		return list;
	}

	/**
	 * 更新内容
	 * (non-Javadoc)
	 * @see com.example.zhangwei.widgts.refreshlist.FreshLayout.PullToRfreshListener#onRefresh()
	 */
	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Thread.sleep(1000);
				} catch (Exception e) {
					// TODO: handle exception
				}
				RefreshListActivity.this.runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						int type = mCustomViewGroup.getCurScreen();
						switch (type) {
						case 0:
							mFreshLayout.stopRefresh();
							break;
						case 1:
							mRefreshList.stopRefresh();
							break;
						case 2:
							mExpandableListView.stopRefresh();
							break;
						default:
							break;
						}
					}
				});
			}
		}).start();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		//添加内容
		int type = mCustomViewGroup.getCurScreen();
		switch (type) {
		case 1:
			
			break;
		case 2:

			break;
		default:
			break;
		}
	}

}
